﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TestPanel
{
   public class AsynTagService
    {
        public string PollingAsyncDelegate(string contentNews)
        {
            AsyncAction aa = new AsyncAction();
            return aa.PollAsyncDelegate(contentNews);
        }

        public class AsyncAction
        {
            public string PollAsyncDelegate(string instring)
            {
                // Set up the delegate
                AsyncInvoke method1 = new AsyncInvoke(TestAsyncInvoke.Method1);
                // Define the AsyncCallback delegate.
                AsyncCallback callBack = new AsyncCallback(TestAsyncInvoke.CallBack);
                IAsyncResult asyncResult = method1.BeginInvoke(instring, callBack, method1);

                while (!asyncResult.IsCompleted)
                {
                    // give up the CPU for 1 second
                    Thread.Sleep(1000);
                    Console.Write('.');
                }
                Console.WriteLine("Finished Polling");

                try
                {
                    return method1.EndInvoke(asyncResult);

                }
                catch (Exception e)
                {
                    return string.Empty;
                }
            }
        }

        public delegate string AsyncInvoke(string instring);

        public class TestAsyncInvoke
        {

            public static string Method1(string inString)
            {
                TagWS.HelloService abc = new TagWS.HelloService();
                return abc.getTagNPL(inString, 3);
            }

            public static void CallBack(IAsyncResult ar)
            {
                // Retrieve the delegate.
                AsyncInvoke ai = (AsyncInvoke)ar.AsyncState;

                // Call EndInvoke to retrieve the results.
                string retVal = ai.EndInvoke(ar);
                Console.WriteLine("retVal: " + retVal);

            }
        }
    }
}
